<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class ThingDB extends BaseDB
{
    /**
     * 评论
     */

    public $tThing = 'thing';
    public $tCate = 'cate';
    public $tCode = 'code';
    public $tVideo = 'video';
    public $tGuoSet = 'guo_set';

    public function __construct()
    {
    }

    /**
     * 我的页面-橱窗-物品列表
     */
    public function getMyThingIndex($wx)
    {
        $query = DB::table($this->tThing)->where('del',0)->where('wx',$wx);
        $total = $query->count();
        $models = $query->orderBy('id','desc')->skip(0)->take(10)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $createTime = date('Y-m-d H:i',$model['created_at']);
            $cates = DB::table($this->tCate)->where('id',$model['cate'])->get();
            $cateOne = count($cates) ? $this->jsonToArr($cates[0]) : '';
            $cateName = $cateOne ? $cateOne['name'] : '';
            $items[] = [
                'id'=>$model['id'], 'name'=>$model['name'],
                'cate'=>$model['cate'], 'cateName'=>$cateName,
                'createTime'=>$createTime,
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items,'total'=>$total);
    }

    /**
     * 品类列表
     */
    public function getCateIndex()
    {
        $models = DB::table($this->tCate)->where('del',0)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $models = $this->jsonToArr($models);
        $items = array();
        foreach ($models as $model) {
            $items[] = [ 'id'=>$model['id'], 'name'=>$model['name'], ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 品类一个
     */
    public function getCateOne($name)
    {
        $models = DB::table($this->tCate)->where('del',0)->where('name',$name)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $model = $this->jsonToArr($models[0]);
        $items = [ 'id'=>$model['id'], 'name'=>$model['name'], ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 品类新增
     */
    public function setCateAdd($wx,$name)
    {
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tCate)->insertGetId(['wx'=>$wx,'name'=>$name,'created_at'=>time(),]);
            if (!$resDb) { return array('code'=>'-2','msg'=>'操作失败'); }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 码获取
     */
    public function getCodeOne($code)
    {
        $models = DB::table($this->tCode)->where('code',$code)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $model = $this->jsonToArr($models[0]);
        $items = [ 'id'=>$model['id'], 'code'=>$model['code'], ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 码生成
     */
    public function setCodeAdd($code)
    {
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tCode)->insertGetId(['code'=>$code]);
            if (!$resDb) { return array('code'=>'-2','msg'=>'操作失败'); }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 物品一个
     */
    public function getOneByCode($code)
    {
        $models = DB::table($this->tThing)->where('del',0)->where('code',$code)->get();
        if (!count($models)) { return array('code'=>'-1','msg'=>'no data'); }
        $model = $this->jsonToArr($models[0]);
        $items = [
            'id'=>$model['id'], 'code'=>$model['code'], 'name'=>$model['name'],
        ];
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 物品添加
     */
    public function setAdd($wx,$utype,$code,$cover,$link,$name,$cate,$year,$out)
    {
//        $cate = 0; //物品品类：1苹果；用户类型：1果农2商家3用户
        if ($utype==1) { $cate = 1; }
        $serial = uniqid(); //物品编号
        //开启事务
        DB::beginTransaction();
        try {
            $lastId = DB::table($this->tThing)->insertGetId([
                'name'=>$name, 'cate'=>$cate, 'code'=>$code, 'serial'=>$serial, 'utype'=>$utype,
                'wx'=>$wx, 'year'=>$year, 'out'=>$out, 'created_at'=>time(),
            ]);
            if (!$lastId) {
                return array('code'=>'-3','msg'=>'物品失败');
            }
            $resVideo = DB::table($this->tVideo)->insertGetId([
                'type'=>$utype, 'wx'=>$wx, 'thing'=>$lastId, 'title'=>uniqid(),
                'cover'=>$cover, 'link'=>$link, 'created_at'=>time(),
            ]);
            if (!$resVideo) {
                return array('code'=>'-2','msg'=>'视频失败');
            }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }
}